/*
 * Playback.c
 *
 * Created: 26.03.2012 16:00:34
 *  Author: Luke
 */ 

#include <util/delay.h>
#include "drivers/nfamp.h"
#include "vs1011/vs1011e.h"
#include "sdfat-rr/sdfat-rr.h"
#include "drivers/led.h"

uint8_t stop_flag_ =  0;
uint8_t next_song_ = 0;

void pbStopPlaybackAsync(void)
{
	stop_flag_ = 1;
}

void pbStartPlayback()
{
	if (stop_flag_)
		stop_flag_ = 0;
		
	if(!sdOpenStream_nnn(next_song_))
	{
		next_song_ = 0;
		_delay_ms(500);
		ledBlinkRed(4);
		return;
	}
	
	ampOn();
	vsUnReset();
	_delay_ms(10);
	
	vsPrepPlay();
	ledBlue();
	while ((sdFillStreamBuffer(playback_buffer_,BUFFERSIZE)) && !stop_flag_)
	{
		vsPlayBuffer();
	}
	ledOff();
	vsReset();
	ampOff();
	sdCloseStream();
	
	if (!stop_flag_)
		next_song_ = 0;
	stop_flag_ = 0;
}

void pbSetNextSong(uint8_t file_num)
{
	next_song_ = file_num;
}

uint8_t pbGetNextSong(void)
{
	return next_song_;
}